const express = require("express")
const router = express.Router()
const {addNavigation,searchNavigation,updateNavigation,deleteNavigation,addSimpleNav} = require("../control/navigation")
const {SuccessModel,ErrorModel} = require("../model/resModel")
const passport = require("passport")

router.post("/add",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await addNavigation(req,res)
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("添加成功"))
  }else{
    res.json(new ErrorModel("添加失败"))
  }
  
})

router.post("/addTest",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await addSimpleNav(req,res)
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("添加成功"))
  }else{
    res.json(new ErrorModel("添加失败"))
  }
  
})



router.post("/del",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await deleteNavigation(req,res)
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("删除成功"))
  }else{
    res.json(new ErrorModel("删除失败"))
  }
})


router.get("/search",async (req,res)=>{
  let data =await searchNavigation(req,res)  
  if(data&&data.length>0){
    res.json(new SuccessModel(data,"查找成功"))
  }else{
    res.json(new ErrorModel("查找失败"))
  }
})
router.get("/chooseSearch",async (req,res)=>{
  let data = await UserSearchProd(req,res)
  if(data&&data.length>0){
    res.json(new SuccessModel(data,"查找成功"))
  }else{
    res.json(new ErrorModel("查找失败"))
  }
})
router.post("/update",async (req,res)=>{
  let data =await updateNavigation(req,res)
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel(data,"更新成功"))
  }else{
    res.json(new ErrorModel("更新失败"))
  }
})



module.exports=router